// api/taskMock.js - 任务相关的模拟数据

// 模拟任务列表数据
const mockTasks = [
  {
    id: '1',
    title: '每日签到',
    subTitle: '签到有奖励',
    description: '连续签到7天可获得额外奖励',
    points: 5,
    category: 1,
    category_text: '每日任务',
    status: 'pending',
    created_at: '2024-01-01T08:00:00Z',
    deadline: '2026-01-08T00:00:00Z',
    valid_from: '2024-01-01T00:00:00Z',
    valid_to: '2026-02-08T00:00:00Z',
    completed_count: 1258,
    max_participants: 10,
    difficulty: '简单',
    is_hot: true
  },
  {
    id: '2',
    title: '完善个人资料',
    subTitle: '让人看到更好的你',
    description: '上传头像并填写个人信息',
    points: 20,
    category: 2,
    category_text: '新手任务',
    status: 'pending',
    created_at: '2024-01-02T09:00:00Z',
    deadline: '2026-01-08T00:00:00Z',
    valid_from: '2024-01-01T00:00:00Z',
    valid_to: '2026-02-08T00:00:00Z',
    completed_count: 892,
    max_participants: 10,
    difficulty: '简单',
    is_hot: false
  },
  {
    id: '3',
    title: '分享到朋友圈',
    subTitle: '分享有奖励',
    description: '将任务分享到朋友圈并截图',
    points: 15,
    category: 3,
    category_text: '分享任务',
    status: 'pending',
    created_at: '2024-01-03T10:00:00Z',
    deadline: '2026-01-08T00:00:00Z',
    valid_from: '2024-01-01T00:00:00Z',
    valid_to: '2026-02-08T00:00:00Z',
    completed_count: 654,
    max_participants: 10,
    difficulty: '中等',
    is_hot: true
  },
  {
    id: '4',
    title: '产品体验调查',
    subTitle: '分享你的使用体验',
    description: '完成一份产品使用体验调查问卷',
    points: 30,
    category: 4,
    category_text: '调查任务',
    status: 'pending',
    created_at: '2024-01-04T11:00:00Z',
    deadline: '2026-01-08T00:00:00Z',
    valid_from: '2024-01-01T00:00:00Z',
    valid_to: '2026-02-08T00:00:00Z',
    completed_count: 432,
    max_participants: 1,
    difficulty: '困难',
    is_hot: true
  },
  {
    id: '5',
    title: '邀请好友注册',
    subTitle: '邀请新用户注册并完成新手任务',
    description: '邀请新用户注册并完成新手任务',
    points: 50,
    category: 5,
    category_text: '长期任务',
    status: 'pending',
    created_at: '2024-01-05T12:00:00Z',
    deadline: '2026-01-08T00:00:00Z',
    valid_from: '2024-01-01T00:00:00Z',
    valid_to: '2026-02-08T00:00:00Z',
    completed_count: 287,
    max_participants: 1,
    difficulty: '中等',
    is_hot: false
  },
  {
    id: '6',
    title: '观看广告视频',
    subTitle: '观看广告视频获得奖励',
    description: '观看完整的广告视频获得奖励',
    points: 10,
    category: 1,
    category_text: '每日任务',
    status: 'pending',
    created_at: '2024-01-06T13:00:00Z',
    deadline: '2026-01-08T00:00:00Z',
    valid_from: '2024-01-01T00:00:00Z',
    valid_to: '2026-02-08T00:00:00Z',
    completed_count: 1890,
    max_participants: 1,
    difficulty: '简单',
    is_hot: true
  },
  {
    id: '7',
    title: '评论产品建议',
    subTitle: '在社区发表对产品的建议或反馈',
    description: '在社区发表对产品的建议或反馈',
    points: 15,
    category: 5,
    category_text: '长期任务',
    status: 'pending',
    created_at: '2024-01-07T14:00:00Z',
    deadline: '2026-01-08T00:00:00Z',
    valid_from: '2024-01-01T00:00:00Z',
    valid_to: '2026-02-08T00:00:00Z',
    completed_count: 534,
    max_participants: 1,
    difficulty: '简单',
    is_hot: false
  },
  {
    id: '8',
    title: '下载并体验应用',
    subTitle: '下载指定应用并体验5分钟',
    description: '下载指定应用并体验5分钟',
    points: 40,
    category: 2,
    category_text: '新手任务',
    status: 'pending',
    created_at: '2024-01-08T15:00:00Z',
    deadline: '2026-01-08T00:00:00Z',
    valid_from: '2024-01-01T00:00:00Z',
    valid_to: '2025-02-08T00:00:00Z',
    completed_count: 321,
    max_participants: 1,
    difficulty: '中等',
    is_hot: true
  }
];

// 根据筛选条件过滤任务
function filterTasks(tasks, params = {}) {
  let filtered = [...tasks];
  
  // 按分类筛选
  if (params.category && params.category !== '') {
    filtered = filtered.filter(task => task.category === params.category);
  }
  
  // 按奖励范围筛选
  if (params.rewardRange && params.rewardRange !== '') {
    if (params.rewardRange === '5-10') {
      filtered = filtered.filter(task => task.points >= 5 && task.points <= 10);
    } else if (params.rewardRange === '10-20') {
      filtered = filtered.filter(task => task.points > 10 && task.points <= 20);
    } else if (params.rewardRange === '20-50') {
      filtered = filtered.filter(task => task.points > 20 && task.points <= 50);
    } else if (params.rewardRange === '50+') {
      filtered = filtered.filter(task => task.points > 50);
    }
  }
  
  // 按关键词搜索
  if (params.keyword && params.keyword.trim() !== '') {
    const keyword = params.keyword.toLowerCase();
    filtered = filtered.filter(task => 
      task.title.toLowerCase().includes(keyword) || 
      task.description.toLowerCase().includes(keyword)
    );
  }
  
  return filtered;
}

// 对任务进行排序
function sortTasks(tasks, sortType) {
  const sorted = [...tasks];
  
  switch (sortType) {
    case 'newest':
      return sorted.sort((a, b) => new Date(b.created_at) - new Date(a.created_at));
    case 'highest_reward':
      return sorted.sort((a, b) => b.points - a.points);
    case 'easiest':
      const difficultyOrder = { '简单': 1, '中等': 2, '困难': 3 };
      return sorted.sort((a, b) => difficultyOrder[a.difficulty] - difficultyOrder[b.difficulty]);
    case 'hottest':
      return sorted.sort((a, b) => b.completed_count - a.completed_count);
    default:
      return sorted;
  }
}

// 分页处理
function paginateTasks(tasks, page, pageSize) {
  const start = (page - 1) * pageSize;
  const end = start + pageSize;
  return {
    list: tasks.slice(start, end),
    total: tasks.length
  };
}

export default {
  mockTasks,
  filterTasks,
  sortTasks,
  paginateTasks
};